package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.NursingProject;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> searchByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.
        PageHelper.startPage(pageNum,pageSize);
        //2.
        Page<NursingProjectVo> page = nursingProjectMapper.searchByPage(name, status);
        //3.
        return PageResponse.of(page,NursingProjectVo.class);
    }

    /**
     * 添加护理项目
     * @param npj
     */
    @Override
    public void addNursing(NursingProjectDto npj) {
        //1.
        NursingProject nursingProject=new NursingProject();
        BeanUtils.copyProperties(npj,nursingProject);
        //2.
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        //3.
        nursingProjectMapper.insertNursingProject(nursingProject);
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProject search(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectNursingProjectById(id);

        return nursingProject;
    }

    /**
     * 修改护理项目
     * @param nursingProjectVo
     */
    @Override
    public void update(NursingProjectDto nursingProjectVo) {
        NursingProject bean = BeanUtil.toBean(nursingProjectVo, NursingProject.class);

        bean.setUpdateTime(LocalDateTime.now());

        nursingProjectMapper.updateNursingProject(bean);
    }
}
